﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using DAL;
using Common;
using System.Data.Linq;
namespace BAL
{
    public class Level
    {
        private static string CacheName = "AllLevel";
        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <returns></returns>
        public static void ToCache()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                CacheHelper.Refurbish(CacheName, db.Levels.ToList(), 0, 60);
            }
        }
        public static int GetFirstId()
        {
            if (!CacheHelper.IsExist(CacheName))
            {
                ToCache();
            }
            IList<Models.Level> cache = (IList<Models.Level>)CacheHelper.GetCache(CacheName);
            return cache.First<Models.Level>().Id;
        }

 

 

        /// <summary>
        /// 检查缓存状态，并读取缓存内容
        /// </summary>
        /// <returns>读取缓存中的内容</returns>
        public static IList<Models.Level> CheckCache()
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            return (IList<Models.Level>)CacheHelper.GetCache(CacheName);
        }

        internal static LevelInfo TransLevel2LevelInfo(Models.Level level)
        {
            LevelInfo levelInfo = new LevelInfo();

            Utility.SetObjectValueWithTheSamePropertyName(levelInfo, level);

            return levelInfo;
        }

        public static LevelInfo AddLevel(LevelInfo levelInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                Models.Level level = new Models.Level();
                Utility.SetObjectValueWithTheSamePropertyName(level, levelInfo);
                DatabaseHelper.Insert<Models.Level>(level);
                ToCache();
                return TransLevel2LevelInfo(level);
            }
        }

        public static void EditLevel(LevelInfo levelInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                try
                {
                    Models.Level level = db.Levels.Single(b => b.Id == levelInfo.Id);

                    Utility.SetObjectValueWithTheSamePropertyName(level, levelInfo);

                    db.SubmitChanges(ConflictMode.ContinueOnConflict);
                    ToCache();
                }
                catch (ChangeConflictException)
                {
                    foreach (ObjectChangeConflict occ in db.ChangeConflicts)
                    {
                        occ.Resolve(RefreshMode.KeepCurrentValues);
                    }
                }
            }
        }
        public void Update(int id, string name)
        {

        }
        public void Delete(int id)
        {

        }
        public static void DelLevel(int id)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                DelLevel(new int[] { id });
            }
        }

        public static void DelLevel(int[] lstId)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                if (lstId.Length > 0)
                {
                    string sql = string.Format("delete from Level where id in ({0})", string.Join(",", lstId.ConvertToStringArray()));
                    db.ExecuteCommand(sql);
                    ToCache();
                }
            }
        }

        public static LevelInfo GetLevelInfoById(int Id)
        {
            IList<Models.Level> Level_Info = CheckCache();
            Models.Level level = Level_Info.Single(b => b.Id == Id);
            return TransLevel2LevelInfo(level);
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    Models.Level level = db.Levels.Single(b => b.Id == Id);
            //    return TransLevel2LevelInfo(level);
            //}
        }
        public static IList<LevelInfo> GetLevelInfos(int first, int max)
        {
            IList<Models.Level> Level_Info = CheckCache();
            if (max > 0)
            {
                return Level_Info.Skip(first).Take(max).Select(c => TransLevel2LevelInfo(c)).ToList();
            }
            else
            {
                return Level_Info.Select(c => TransLevel2LevelInfo(c)).ToList();
            }
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    if (max > 0)
            //    {
            //        return db.GetTable<Models.Level>().Skip(first).Take(max).Select(c => TransLevel2LevelInfo(c)).ToList();
            //    }
            //    else
            //    {
            //        return db.GetTable<Models.Level>().Select(c => TransLevel2LevelInfo(c)).ToList();
            //    }
            //}
        }

        public static int GetLevelInfoCount()
        {
            IList<Models.Level> Level_Info = CheckCache();
            return Level_Info.Count();
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.GetTable<Models.Level>().Count();
            //}
        }
        public static bool CheckLevelExistById(int Id)
        {
            IList<Models.Level> Level_Info = CheckCache();
            return Level_Info.Where(w => w.Id == Id).Count() > 0;
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.Levels.Where(w => w.Id == Id).Count() > 0;
            //}
        }
    }
}
